package com.xxadmin.usercenter.controller;

import com.github.pagehelper.Page;
import com.xxadmin.common.dto.PageInfoDTO;
import com.xxadmin.common.vo.ResponseModelBuilder;
import com.xxadmin.common.util.clone.CopyUtils;
import com.xxadmin.common.util.page.PageUtils;
import com.xxadmin.common.vo.PageInfoVO;
import com.xxadmin.common.vo.ResponseModel;
import com.xxadmin.common.web.BaseController;
import com.xxadmin.usercenter.domain.OauthClientDetails;
import com.xxadmin.usercenter.dto.OauthClientDetailsDTO;
import com.xxadmin.usercenter.dto.OauthUserQueryDTO;
import com.xxadmin.usercenter.service.OauthClientDetailsService;
import com.xxadmin.usercenter.vo.request.OauthClientDetailsVO;
import com.xxadmin.usercenter.vo.request.OauthUserQueryVO;
import com.xxadmin.usercenter.vo.response.OauthClientDetailsResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 授权client
 * 
 * @author:chenming
 * @date:2018/8/22
 */
@Api(tags = {"授权 client"})
@RequestMapping("/oauthClientDetails")
@RestController
public class OauthClientDetailsController extends BaseController {

    @Autowired
    private OauthClientDetailsService oauthClientDetailsService;

    /**
     * 新增 OauthUserDetail
     *
     */
    @ApiOperation(value = "新增授权 client" ,notes = "新增授权 client")
    @PostMapping("/add")
    public ResponseModel addOauthUserDetail(@RequestBody OauthClientDetailsVO oauthClientDetails){

        super.validate(oauthClientDetails);

        OauthClientDetailsDTO dto = CopyUtils.copyBean(oauthClientDetails,OauthClientDetailsDTO.class);

        oauthClientDetailsService.add(dto);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }

    /**
     * 修改 OauthUserDetail
     *
     */
    @ApiOperation(value = "修改授权 client" ,notes = "修改授权 client")
    @PutMapping("/update")
    public ResponseModel updateOauthUserDetail(@RequestBody OauthClientDetailsVO oauthClientDetails){
        super.validate(oauthClientDetails);

        OauthClientDetailsDTO dto = CopyUtils.copyBean(oauthClientDetails,OauthClientDetailsDTO.class);

        oauthClientDetailsService.update(dto);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }

    /**
     * 查询授权 client 信息
     *
     */
    @ApiOperation(value = "查询授权 client" ,notes = "查询授权 client")
    @GetMapping("/listOauthUserDetail")
    public ResponseModel<PageInfoVO> listOauthUserDetail(OauthUserQueryVO oauthUserQuery){
        OauthUserQueryDTO queryDTO = CopyUtils.copyBean(oauthUserQuery,OauthUserQueryDTO.class);

        PageInfoDTO<OauthClientDetailsDTO> page = oauthClientDetailsService.listOauthUserDetail(queryDTO);

        PageInfoVO<OauthClientDetailsResponseVO> pageInfo = PageUtils.getPageInfoVO(page,OauthClientDetailsResponseVO
                .class);

        return ResponseModelBuilder.genericSuccessResponseModal(pageInfo);
    }

    @GetMapping("/get/{clientId}")
    public ResponseModel<OauthClientDetailsResponseVO> getOauthClientDetails(@PathVariable("clientId") String clientId){
        OauthClientDetails details = oauthClientDetailsService.getOauthClientDetails(clientId);

        OauthClientDetailsResponseVO detailsVO = CopyUtils.copyBean(details,OauthClientDetailsResponseVO.class);

        return ResponseModelBuilder.genericSuccessResponseModal(detailsVO);
    }
}
